package org.medicalinstrument.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.medicalinstrument.bean.Customer;
import org.medicalinstrument.bean.CustomerAddress;
import org.medicalinstrument.dao.CustomerDao;
import org.medicalinstrument.impl.CustomerDaoImpl;

import java.sql.SQLException;
import java.util.List;

/**
 * @desc ： CustomerService
 * @author : llh
 * @param :
 * @return :
**/
public class CustomerService {
    CustomerDao ci = new CustomerDaoImpl();
    Customer customer;
    String returnString=null;
    List customerlist = null;
    int modifyResult=0;

    public String CustomerLogin (String custNo, String pass) throws SQLException {
        customer = ci.customerLogin(custNo, pass);
        String result=null;
        if(null != customer){
            result = JSONObject.fromObject(customer).toString();
        }else{
            result = "loginfalse";
        }
        return result;
    }

    public String isExist(String custNo) throws SQLException {
        customer=ci.isExist(custNo);
        if(null != customer){
            returnString="exist";
        }
        else{
            returnString="notexist";
        }
        return returnString;


    }


    public String register(String custPhone, String custName, String gender, String custAddr, int custAge, String connect, String pwd, String custNo) throws SQLException {
        return returnString=ci.register(custPhone,custName,gender,custAddr,custAge,connect,pwd,custNo);
    }


    public String getAddress(String accountNo)  throws SQLException {
        CustomerAddress address = ci.getAddress(accountNo);
        if (null != address ){
            returnString = JSONObject.fromObject(address).toString();
        }else{
            returnString = "address_null";
        }
        return returnString;
    }

    public String addAddress(CustomerAddress address) throws SQLException {
        System.out.println("ss "+ address);
        int count = ci.addAddress(address);
        if(count>0){
            return "add_success";
        }else{
            return "add_error";
        }
    }

    public String getAllCustomerList() throws SQLException{
        customerlist =  ci.getAllCustomerList();
        if(customerlist.size() <= 0){
            returnString = "nocustomer";
        }else{
            returnString = JSONArray.fromObject(customerlist).toString();
        }
        return returnString;
    }

    public String Search(String accountNo, String custPhone) throws SQLException{
        customerlist =  ci.Search(accountNo,custPhone);
        if(customerlist.size() <= 0){
            returnString = "nocustomer";
        }else{
            returnString = JSONArray.fromObject(customerlist).toString();
        }
        return returnString;
    }


    public String modifycustomer(String custPhone, String accountNo, String password, String consigeeAddr, String custName, String sex, String age) throws SQLException{
        modifyResult = ci.modifycustomer(custPhone,accountNo,password,consigeeAddr,custName,sex,age);
        if (modifyResult <= 0) {
            returnString = "nocustomer";
        } else {
            returnString = "success";
        }
        return returnString;
    }

    public String removeCustomer(String accountNo) {
        try {
            if(ci.delete(accountNo)){
                returnString = "del_success";
            }else{
                returnString = "del_failed";
            }
        } catch (SQLException e) {
            returnString = "del_failed";
            e.printStackTrace();
        }
        return returnString;
    }

    public String deleteOrder (String orderId, String accountNo, String productQR)throws SQLException{
        if(ci.deleteOrder(orderId,accountNo,productQR)){
            return "delete_success";
        }else{
            return "delete_error";
        }

    }
}
